package conf

import (
	"gopkg.in/yaml.v3"
	"os"
)

type config struct {
	AppConf *appConf `yaml:"app"`
	LogConf *logConf `yaml:"log"`
	Pgsql   *pgsql   `yaml:"pgsql"`
	Redis   *redis   `yaml:"redis"`
}

type appConf struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Env      string `yaml:"env"`
	RootPath string `yaml:"rootPath"`
	BaseUrl  string `yaml:"baseUrl"`
	FontPath string `yaml:"fontPath"`
}

type logConf struct {
	LogPath string `yaml:"logPath"`
	LogSize int64  `yaml:"logSize"`
}

type pgsql struct {
	Host   string `yaml:"host"`
	Port   string `yaml:"port"`
	Usname string `yaml:"usname"`
	Passwd string `yaml:"passwd"`
	Dbname string `yaml:"dbname"`
}

type redis struct {
	IpPort string `yaml:"ipPort"`
	Passwd string `yaml:"passwd"`
	DbNum  int    `yaml:"dbNum"`
}

// Conf 全局变量
var Conf *config

// InitConf 读取配置文件内容
func InitConf(confPath string) {
	// 读取文件
	bts, err := os.ReadFile(confPath)
	if err != nil {
		panic("读取配置文件【" + confPath + "】失败。" + err.Error())
	}
	// 解析文件
	if err = yaml.Unmarshal(bts, &Conf); err != nil {
		panic("解析配置文件内容失败。" + err.Error())
	}
}
